package com.wgl.express.UI;

import com.wgl.express.entity.Package;
import com.wgl.express.entity.User;
import com.wgl.express.service.Impl.PackageServiceImpl;
import com.wgl.express.service.Impl.UserServiceImpl;
import com.wgl.express.service.PackageService;
import com.wgl.express.service.UserService;
import com.wgl.express.util.Util;

import java.util.ArrayList;
import java.util.List;

import static com.wgl.express.text.Text.*;

public class Mine {
    private Util util;
    private List<Package> sent;
    private List<Package> sentAll;
    private List<Package> receive;
    private List<Package> receiveAll;

    public void start(){
        util=  Util.getUtil();

        loadPackages();

        eventLoop();
    }

    private void eventLoop(){
        boolean exit = false;
        int choose;

        while (!exit) {
            util.say(TITLE);
            util.say("Hello! " + UserServiceImpl.currentUser.getUsername() + "\n");
            util.say(MENU_MINE);
//            util.readLine();
            if ((choose = util.readInt()) == -1) continue;
            switch (choose) {
                case 0:
                    exit = true;
                    break;
                case 1:
                    buildPackage();
                    break;
                case 2:
                    sentPackage();
                    break;
                case 3:
                    receivedPackage();
                    break;
                case 4:
                    changePassword();
                    break;
                case 5:
                    changePhone();
                    break;

            }
        }
    }

    private void changePhone() {
        User u = UserServiceImpl.currentUser;
        int choose;
        boolean exit = false;
        util.readLine();
        util.say("===修改号码===\n");
        util.say("请输入号码：");
        String p = util.readLine();
        do {
            util.say("确认修改为："+p+"?\n1.\t确认\t0.\t取消\n");
            if((choose = util.readInt())==-1) continue;
            if (choose == 1) {
                UserService service = new UserServiceImpl();
                u.setPhone(p);
                int i = service.setPhone(u);
                if (i == 1) util.say("修改号码成功！\n");
                else util.say("修改号码失败！\n");
            }
                exit = true;
        }while (!exit);
    }

    private void changePassword() {
        User u = UserServiceImpl.currentUser;
        int choose;
        boolean exit = false;
        util.readLine();
        util.say("===修改密码===\n");
        util.say("请输入密码：");
        String p = util.readLine();
        do {
            util.say("确认修改为："+p+"?\n1.\t确认\t0.\t取消\n");
            if((choose = util.readInt())==-1) continue;
            if (choose == 1){
                UserService service = new UserServiceImpl();
                u.setPassword(p);
                int i = service.setPassword(u);
                if (i==1) util.say("修改密码成功！\n");
                else util.say("修改密码失败！\n");
            }
                exit = true;

        }while (!exit);

    }

    private void receivedPackage() {
        receive = new ArrayList<>();
        PackageService service = new PackageServiceImpl(2);
        int frameCount = service.getFrameCount();
        receiveAll = new ArrayList<>();
        int current = 1;
        receive = service.getPackBytophone(UserServiceImpl.currentUser.getPhone());
        receiveAll.addAll(receive);

        int c;
        boolean exit = false;

        do {
            util.say("第 " + current + " 页 / 共 " + frameCount + " 页\n");
            receive.forEach((p) -> {
                util.showPackInfo(p, true);
            });
            util.say(MENU_NEXTPAGE);
            c = util.readInt();

            switch (c) {
                case 1:
                    if (current != frameCount) {
                        receive = service.getPackBytophone(UserServiceImpl.currentUser.getPhone());
                        current += 1;
                    }
                    break;
                case 0:
                    exit = true;
                    break;
            }
        } while (!exit);
    }

    private void sentPackage() {
        sent = new ArrayList<>();
        PackageService service = new PackageServiceImpl(2);
        int frameCount = service.getFrameCount();
        sentAll = new ArrayList<>();
        int current = 1;
        sent = service.getPackByfromphone(UserServiceImpl.currentUser.getPhone());
        sentAll.addAll(sent);

        int c;
        boolean exit = false;

        do {
            util.say("第 " + current + " 页 / 共 " + frameCount + " 页\n");
            sent.forEach((p) -> {
                util.showPackInfo(p, true);
            });
            util.say(MENU_NEXTPAGE);
            c = util.readInt();

            switch (c) {
                case 1:
                    if (current != frameCount) {
                        sent = service.getPackByfromphone(UserServiceImpl.currentUser.getPhone());
                        current += 1;
                    }
                    break;
                case 0:
                    exit = true;
                    break;
            }
        } while (!exit);
    }

    private void buildPackage() {
        Package pack = new Package();
        int ch = -1;
        int res = 0;

        util.say(LINE);
        util.say(TITLE_NEW_PACKAGE);

        util.readLine();
        util.say("寄件人姓名：");
        pack.setFromname(util.readLine());
        util.say("寄件人号码：");
        pack.setFromphone(util.readLine());
        util.say("寄件人地址：");
        pack.setFromaddress(util.readLine());
        util.say("收件人姓名：");
        pack.setToname(util.readLine());
        util.say("收件人号码：");
        pack.setTophone(util.readLine());
        util.say("收件人地址：");
        pack.setToaddress(util.readLine());

        util.say(LINE);
        util.say("请确认寄件信息：" + "\n");
        util.say(LINE);

        util.showPackInfo(pack, true);

        util.say(LINE);

        do {
            util.say("确认信息无误？\n1. 确定\t0. 取消\t");
            ch = util.readInt();
            if (ch == 0) return;
        } while (ch == -1);

        PackageService packageService = new PackageServiceImpl(1);
        res = packageService.addPack(pack);
        if (res == 1) {
            util.say("新建包裹成功！");
        } else {
            util.say("新建包裹失败！");
        }


    }

    private void loadPackages() {

    }


}
